package com.ttmall.product.web;

import com.ttmall.product.entity.CategoryEntity;
import com.ttmall.product.service.CategoryService;
import com.ttmall.product.vo.Catelog2Vo;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RSemaphore;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Controller
public class IndexController {

    @Autowired
    CategoryService categoryService;

    @Autowired
    RedissonClient redissonClient;

    @Autowired
    StringRedisTemplate redisTemplate;


    @GetMapping(value={"/","/index.html"})
    public  String indexPage(Model model){

        // 1. 查出所有一级分类
        List<CategoryEntity> categories = categoryService.getLevel1Categories();
        model.addAttribute("categories" , categories);
        return "index";
    }

    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<String , List<Catelog2Vo>> getCatalogJson(){

        Map<String , List<Catelog2Vo>> map = categoryService.getCatalogJson();
        return map;
    }

    @ResponseBody
    @GetMapping("/hello")
    public  String hello(){
        // 获取锁，只要锁的名字一样，就是同一把锁
        RLock lock = redissonClient.getLock("r-lock");
        // 加锁
        lock.lock(); // 阻塞式等待。 默认加的锁都是30s

        //lock.lock(10, TimeUnit.SECONDS);

        // 锁的自动续期， 如果业务超长，运行期间自动给锁续上新的30s。 不用担心业务时间长，锁自动过期被删掉
        // 加锁的业务只要运行完成， 就不会给当前锁续期，即时不手动解锁，锁默认在30s以后自动删除
        try {
            System.out.println("加锁成功。。thread"+ Thread.currentThread().getId() );
            Thread.sleep(30000);
        }
        catch (Exception ex){
        }
        finally {
            // 解锁
            System.out.println("释放锁。。thread"+ Thread.currentThread().getId() );
            lock.unlock();
        }
        return "hello";
    }

    @ResponseBody
    @RequestMapping("write")
    public  String writeLock(){
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("rw-lock");
        RLock rLock = readWriteLock.writeLock();
        rLock.lock();
        String  uuid = "";
        try {
            uuid = UUID.randomUUID().toString();
            redisTemplate.opsForValue().set("rwvalue" , uuid);
            Thread.sleep(30000);
        }
        catch (Exception e){

        }
        finally {
            rLock.unlock();
        }
        return uuid;
    }
    @ResponseBody
    @RequestMapping("read")
    public  String readLock(){
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("rw-lock");
        RLock rLock = readWriteLock.readLock();
        rLock.lock();
        String s = "";
        try {
            s = redisTemplate.opsForValue().get("rwvalue");
        }
        catch (Exception e){
        }
        finally {
            rLock.unlock();
        }
        return s;
    }

    @ResponseBody
    @RequestMapping("park")
    public  String park() throws InterruptedException {
        RSemaphore park = redissonClient.getSemaphore("park");
        park.acquire();
        return "ok";
    }
    @ResponseBody
    @RequestMapping("go")
    public  String go() throws InterruptedException {
        RSemaphore park = redissonClient.getSemaphore("park");
        park.release();
        return "go";
    }
}
